/**
 * Created by dale on 2017/10/9.
 */
$(document).ready(function () {
    var targetObj = null;

    var um = UM.getEditor('myEditor',{
        imageUrl:baseUrl+"frontend/article/uploadImgs", //处理图片上传的接口
        imageFieldName:"upfile", //上传图片的表单的name
        toolbar:['source | undo redo | bold italic underline strikethrough | superscript subscript | forecolor backcolor | removeformat |',
            'insertorderedlist insertunorderedlist | selectall cleardoc paragraph | fontfamily fontsize' ,
            '| justifyleft justifycenter justifyright justifyjustify |',
            'link unlink | image | accessory ',
            '| horizontal  formula'],
        imagePath : ''
    });

    $('[name=sTag]').change(function () {
        var val = $(this).val();
        var str = '';
        if (val == 1) { //校际
            str ='<td style="width: 75px"><label>参与学校</label></td>'+
                '<td colspan="3"><select type="text" name="sTargetIds[]" multiple class="form-control" style="width: 480px;"></select></td>';
        }else if(val==2) { //校内
            str ='<td style="width: 75px"><label>参与年级</label></td>'+
                '<td colspan="3"><select type="text" name="sTargetIds[]" multiple class="form-control" style="width: 480px;"></select></td>';
        }else if (val == 3){ //班级
            str ='<td style="width: 75px"><label>参与班级</label></td>'+
                '<td  colspan="3"><select type="text" name="sTargetIds[]" multiple class="form-control" style="width: 480px;"></select></td>';
        }
        if (targetObj == null) {
            targetObj = $(str);
        }else {
            targetObj.remove();
            targetObj = $(str);
        }
        $(this).parent().after(targetObj);
        fillSelect(val);
        $('[name="sTargetIds[]"]').select2();
    });
    function fillSelect(val) {
        var  data = null;
        if (val == 1) {
            data = targets['schools'];
        }else  if(val == 2) {
            data = targets['grades'];
        }else if (val == 3) {
            data = targets['classes'];
        }else {
            return  false;
        }
        var str = '<option value="">请选择</option>';
        $.each(data , function (id,text) {
            str+='<option value="'+id+'">'+text+'</option>';
        });
        $('[name="sTargetIds[]"]').empty().append(str);
    }

    var form = new xhForm($('#xh_form'));
    form.parse();
    $('[name=iUserId],[name="sSubject[]"]').select2();
    $(".form-date").datetimepicker({format: 'yyyy-mm-dd hh:ii:00', language: 'zh-CN', minView: 1, autoclose: true});
    if(info!=null){
        form.fill(info);
        um.ready(function() {//编辑器初始化完成再赋值
            if(info['mText']!=null){
                um.setContent(info['mText']);
            }
        });
        $('[name=sTag]').val(info['sTag']).trigger('change');
        $('[name=iUserId]').val(info['iUserId']).trigger('change');
        if(info['sTargetIds'] != null){
            $('[name="sTargetIds[]"]').val(info['sTargetIds'].split(',')).trigger('change');
        }
        if (info['sSubject'] != null) {
            $('[name="sSubject[]"]').val(info['sSubject'].split(',')).trigger('change');
        }
    }

    form.bindRequired($('#bt_modal_confirm',parent.document));
    $('#bt_modal_confirm',parent.document).click(function () {
        form.submit('article/saveActivity',{},function (res) {
            window.top.xhTop.showSuccess(res.msg);
            try {
                window.top.xhTop.getActiveFrame()['activityFrame'].obj.dataGrid.reload();
            }catch (e) {
                window.top.xhTop.getActiveFrame().obj.dataGrid.reload();
            }

            window.top.xhTop.closeModal();
        });
    });

});